const Symptom = require("../models/Symptom");
const SymptomSub = require("../models/SymptomSub");
module.exports = class SymptomManager {
  /**
   * 增加一个Symptom
   * @param {*} obj
   * @returns
   */
  static async add(obj) {
    const result = await Symptom.create(obj);
    return result.toJSON();
  }
  /**
   * 批量新增Symptom
   * @param {*} obj
   * @returns
   */
  static async bulkCreate(valueArr) {
    const result = await Symptom.bulkCreate(valueArr);
    return result;
  }

  /**
   * 删除一个Symptom
   * @param {*} id
   */
  static async delete(id) {
    const result = await Symptom.destroy({
      where: {
        id,
      },
    });
  }
  /**
   * 删除所有Symptom
   * @param {*}
   */
  static async deleteAll() {
    const result = await Symptom.destroy({
      where: {},
    });
  }
  /**
   * 更新Symptom
   * @param {*} Id
   * @param {*} Obj
   */
  static async update(Id, obj) {
    const result = await Symptom.update(obj, {
      where: {
        id: Id,
      },
    });
  }

  /**
   * 根据id查询Symptom
   * @returns
   */
  static async getById(id) {
    const result = await Symptom.findOne({
      where: {
        id,
      },
    });
    if (result === null) {
      console.log("Not found!");
    } else {
      return JSON.parse(JSON.stringify(result));
    }
  }

  /**
   * 查询所有的Symptom
   * @returns
   */
  static async getAll() {
    const result = await Symptom.findAll({
      include: [
        {
          model: SymptomSub,
        },
      ],
    });
    const res = JSON.parse(JSON.stringify(result));
    const newRes = [];
    if (res && res.length > 0) {
      res.forEach((item) => {
        const obj = {};
        obj.subject = item.symptomsub.title;
        obj.symptom = item.title;
        obj.seen = item.seen;
        obj.tips = item.tips;
        newRes.push(obj);
      });

      return newRes;
    } else {
      return null;
    }
  }
  /**
   * 查询所有的Symptom
   * @returns
   */
  static async getSymptomByTitle(title) {
    const result = await Symptom.findAll({
      where: {
        title,
      },
    });
    if (result.length >= 1) {
      const sys = result[0];
      return sys;
    } else {
      return result;
    }
  }
};
